<template>
  <div class="app-container">
    <el-tabs v-model="activeTab">
      <el-tab-pane label="相关性定义" name="1">
        <el-table v-loading="loading" :data="tableData">
          <el-table-column label="相关性" prop="correl"></el-table-column>
          <el-table-column label="类型">
            <template #default="scope">
              <el-select v-model="scope.row.type" placeholder="请选择" :disabled="pageType ==='look'">
                <el-option v-for="item in valueTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
              <el-input v-show="scope.row.type ==='4'" placeholder="请输入码表名" />
            </template>
          </el-table-column>
        </el-table>
        <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
        />
        <div style="text-align: center;margin-top: 24px;">
          <el-button type="primary" @click="saveData">保存</el-button>
        </div>
      </el-tab-pane>
      <!-- <el-tab-pane label="参数码表" name="2">
          <paramCode />
      </el-tab-pane> -->
    </el-tabs>
  </div>
</template>

<script setup name="CorrelDefine">
import { ElMessage } from 'element-plus';
// import paramCode from "./paramCode.vue";
import { defineQuery, defineSave, parameterSave} from '@/api/safeguard/index.js'
const route = useRoute();

const listId = route.params && route.params.listId
const {
  from: pageType,
  name,
  type
} = route.query

const activeTab = ref('1')
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  listId: listId
})
const total = ref(0);
const loading = ref(false)

const valueTypeOptions = ref([
  {label: 'number', value: '1'},
  {label: 'string', value: '2'},
  {label: 'date', value: '3'},
  {label: '码表', value: '4'},
])
const typeOptions = ref([
  {label: '请选择', value: ''},
  {label: '区间', value: '1'},
  {label: '码值', value: '2'},
])
const tableData = ref([
  {correl: name, type: type}
])

initLoad()
function initLoad () {
  queryParams.value.pageNum = 1;
  getList()
}

function getList () {
  loading.value = true;
  const data = {
    ...queryParams.value  
  }
  defineQuery(data).then(res => {
    tableData.value = res.data || []
    total.value = res.total || 0
  }).finally(() => {
    loading.value = false;
  })
}

function saveData () {
  parameterAdd({data: tableData.value}).then(res => {
    ElMessage({  
        message: '保存成功',  
        type: 'success',  
      }); 
    getList()
  })
}
</script>
